{{!

  Copyright (c) Meta Platforms, Inc. and affiliates.

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.

}}
{{#import "method_decorators/params" as params}}

{{!
  handler_functions - These are the functions that help execute the
  decorator functions.
}}
{{#let partial handler_functions |leaf_service service| captures |params|}}
{{#if (object.eq? service.extends null)}}{{#else}}{{#partial handler_functions leaf_service=leaf_service service=service.extends}}{{/if (object.eq? service.extends null)}}
{{#each service.functions as |function|}}

void apache::thrift::ServiceHandler<{{leaf_service.qualified_name}}>::fbthrift_execute_decorators_before_{{function.cpp_name}}(
  apache::thrift::Cpp2RequestContext& requestCtx{{#each function.params.fields as |param|}}, {{#partial params.param_type param=param}} {{#partial params.param_name param=param}}{{/each}}) {
  std::for_each(fbthrift_methodDecorators_.begin(), fbthrift_methodDecorators_.end(), [&](auto& decorator) {
    ServiceMethodDecoratorBase::BeforeParams beforeParams{detail::Cpp2RequestContextUnsafeAPI(requestCtx).getDecoratorDataMutable()};
    decorator->before_{{function.cpp_name}}(std::move(beforeParams){{#each function.params.fields as |param|}}, {{#partial params.param_name param=param}}{{/each}});
  });
}

void apache::thrift::ServiceHandler<{{leaf_service.qualified_name}}>::fbthrift_execute_decorators_after_{{function.cpp_name}}(
  apache::thrift::Cpp2RequestContext& requestCtx{{#partial params.after_param service=service function=function}}) {
  std::for_each(fbthrift_methodDecorators_.begin(), fbthrift_methodDecorators_.end(), [&](auto& decorator) {
    ServiceMethodDecoratorBase::AfterParams afterParams{detail::Cpp2RequestContextUnsafeAPI(requestCtx).getDecoratorDataMutable()};
    decorator->after_{{function.cpp_name}}(std::move(afterParams){{#partial params.after_invocation_args function=function}});
  });
}
{{/each}}
{{#each service.interactions as |interaction|}}{{#each interaction.functions as |function|}}

void apache::thrift::ServiceHandler<{{leaf_service.qualified_name}}>::fbthrift_execute_decorators_before_{{interaction.name}}_{{function.cpp_name}}(
  apache::thrift::Cpp2RequestContext& requestCtx{{#each function.params.fields as |param|}}, {{#partial params.param_type param=param}} {{#partial params.param_name param=param}}{{/each}}) {
  std::for_each(fbthrift_methodDecorators_.begin(), fbthrift_methodDecorators_.end(), [&](auto& decorator) {
    ServiceMethodDecoratorBase::BeforeParams beforeParams{detail::Cpp2RequestContextUnsafeAPI(requestCtx).getDecoratorDataMutable()};
    decorator->before_{{interaction.name}}_{{function.cpp_name}}(std::move(beforeParams){{#each function.params.fields as |param|}}, {{#partial params.param_name param=param}}{{/each}});
  });
}

void apache::thrift::ServiceHandler<{{leaf_service.qualified_name}}>::fbthrift_execute_decorators_after_{{interaction.name}}_{{function.cpp_name}}(
  apache::thrift::Cpp2RequestContext& requestCtx{{#partial params.after_param service=service function=function}}) {
  std::for_each(fbthrift_methodDecorators_.begin(), fbthrift_methodDecorators_.end(), [&](auto& decorator) {
    ServiceMethodDecoratorBase::AfterParams afterParams{detail::Cpp2RequestContextUnsafeAPI(requestCtx).getDecoratorDataMutable()};
    decorator->after_{{interaction.name}}_{{function.cpp_name}}(std::move(afterParams){{#if function.return_type.void?}}{{#else}}, result{{/if function.return_type.void?}});
  });
}
{{/each}}{{/each}}
{{/let partial}}

{{!
  decorator_definitions - these are additional function definitions for
  helpers generated by the addition of method decorators.
}}
{{#let export partial decorator_definitions |service| captures |handler_functions|}}
{{#if service.interaction?}}{{#else}}{{! None of this applies to interaction handlers}}{{#if service.cpp_requires_method_decorator?}}

std::vector<std::reference_wrapper<apache::thrift::ServiceMethodDecoratorBase>> apache::thrift::ServiceHandler<{{service.qualified_name}}>::fbthrift_getDecorators() {
  std::vector<std::reference_wrapper<apache::thrift::ServiceMethodDecoratorBase>> out;
  for (auto& decoratorPtr : fbthrift_methodDecorators_) {
    out.emplace_back(std::ref(*decoratorPtr));
  }
  return out;
}
{{#partial handler_functions leaf_service=service service=service}}

void apache::thrift::decorate(
    apache::thrift::ServiceHandler<{{service.qualified_name}}>& handler,
    apache::thrift::ServiceMethodDecoratorList<{{service.qualified_name}}> decorators) {
  handler.fbthrift_methodDecorators_ = std::move(decorators);
}
{{/if service.cpp_requires_method_decorator?}}{{/if service.interaction?}}
{{/let partial}}
